/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MaterialPack;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import EntityGameObject.*;
import Supporter.ImageSupporter;
import javax.microedition.lcdui.game.Sprite;

import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Timer;
import java.util.TimerTask;
/**
 *
 * @author Van persie
 */
public class PetInstance extends MaterialObject {
    
    int state = 0;
    Pet pet = null;
    Sprite NormalSprite = null;
    Sprite EmotionSad = null;
    Sprite EmotionFun = null;
    Sprite StatusHungry = null;
    Sprite StatusFull = null;
            
    int current = -1;
    
    int mode = -1;
    
    
    long LastPlay;
    long LastFeed;
    ContextDialog [] PetInfo = null;
    //ContextDialog PetInfo = null;
    int UsedBubble = 0;
    
    public PetInstance(String pathPhoto, int x, int y, int wid, int hei , Pet pascal)
    {
        super.GetGraphicsInfo(x, y, wid, hei);
        
        NormalSprite = LoadSprite(pathPhoto, wid, hei, 2);
        
        
        pet = pascal;
        PetInfo = new ContextDialog[5];
        //PetInfo = new ContextDialog("/Bubble.png");
//        PetInfo = new Vector();
        ImageSupporter imSpp = new ImageSupporter();
        Image temp = ImageSupporter.transparent(imSpp.getImage("/Bubble.png"));
        for(int i = 0;  i < PetInfo.length ; i++)
        {
            PetInfo[i] = new ContextDialog(temp);
//            PetInfo[i] = new ContextDialog("/Bubble.png");
           // PetInfo[i].TransBackground(true);
        }
        UsedBubble = 0;
        
        LastFeed = System.currentTimeMillis();
        LastPlay = System.currentTimeMillis();
        
        isVisible = true;
    }
    public Sprite LoadSprite(String pathImg, int widframe,int heiframe, int n )
    {
        ImageSupporter imgSupp = new ImageSupporter();
        Image temp = imgSupp.getImage(pathImg);
        temp = ImageSupporter.scaleImage(temp, widframe*n, heiframe);
        temp = ImageSupporter.transparent(temp);
        
        Sprite spr = new Sprite(temp, widframe, heiframe);
        return spr;
    }
    public void Feed(int typeFeed, int val)
    {
        switch(typeFeed)
        {
            case Pet.EXP:
                PetInfo[UsedBubble].SetAnchor(ContextDialog.HORIZON_MIDDLE,ContextDialog.VERTICAL_MIDDLE);
                PetInfo[UsedBubble].Show("+" + Integer.toString(val) + " EXP", xPos + width/2,yPos + height/2, 60, 60, 50,1);
//                ((ContextDialog)PetInfo.elementAt(UsedBubble)).SetAnchor(ContextDialog.HORIZON_MIDDLE,ContextDialog.VERTICAL_MIDDLE);
//                ((ContextDialog)PetInfo.elementAt(UsedBubble)).Show("+" + Integer.toString(val) + " EXP", xPos + width/2,yPos + height/2, 60, 60, 50,1);
                //PetInfo.Show(background, val, val, width, val, state, val);
                // add
                if(UsedBubble < PetInfo.length)
                    UsedBubble ++;
                break;
        }
        
    }
    public void Play()
    {
    }
    public void Update()
    {
        long current = System.currentTimeMillis();
        
        long HungryTime = (current - LastFeed)/60000;
        long BoringTime = (current - LastPlay)/60000;
        if(HungryTime >= 20)
        {
            if(pet.getEmotion() != Pet.EMOTION_SAD)
                pet.setEmotion(Pet.EMOTION_SAD);
        }
        if(BoringTime >= 20)
        {
            if(pet.getStatus() != Pet.STATUS_HUNGRY)
                pet.setStatus(Pet.STATUS_HUNGRY);
        }
        if(PetInfo != null)
            for(int i = 0; i < UsedBubble; i++)
                if(PetInfo[i].MyUpdate())
                {
                    ContextDialog temp = PetInfo[i];
                    PetInfo[i] = PetInfo[UsedBubble-1];
                    PetInfo[UsedBubble-1] = temp;
                    UsedBubble--;
                }
                    
        //PetInfo.MyUpdate();
        
    }
    public void paint(Graphics g)
    {
        if(isVisible)
        {            
            //g.drawImage(background, getxPos(), getyPos(), Graphics.TOP | Graphics.LEFT);
            
            NormalSprite.setPosition(xPos, yPos);
            NormalSprite.paint(g);

            if(PetInfo != null)
            for(int i = 0; i < UsedBubble; i++)
                PetInfo[i].paint(g);
           // PetInfo.paint(g);
        }
    }
    public boolean isContained(int x, int y)
    {
        if( x > getxPos() && x < (getxPos() + getWidth()) && y > getyPos() && y < (getyPos() + getHeight()))
            return true;
        return false;
    }
    

    public boolean Press(int x, int y)
    {
        if(isContained(x, y)){
            current = ((current + 1)%NormalSprite.getFrameSequenceLength());
            NormalSprite.setFrame(current);
            state = 1;
            return true;
        }
        return false;
    }
    public boolean Release(int x, int y)
    {
        if(state !=0)
        {
            current = (current + 1)%NormalSprite.getFrameSequenceLength();
            NormalSprite.setFrame(current);
            state = 0;
            return true;
        }
        return false;
    }      
    public void Add(PetItem item)
    {
        
    }
    
    public void BuyItem(PetItem item)
    {
        pet.AddAttributes(Pet.MONEY, -item.getPrice());
        pet.AddAttributes(Pet.EXP, item.getIncrEXP());
        pet.AddAttributes(Pet.POINTS, item.getIncrPoints());
        pet.AddAttributes(Pet.SPEED, item.getIncrSpeed());
        pet.AddAttributes(Pet.TONGUELEN, item.getIncrTongueLen());
    }
    public void ReturnItem(PetItem item)
    {
        pet.AddAttributes(Pet.MONEY, item.getPrice());
        pet.AddAttributes(Pet.EXP, -item.getIncrEXP());
        pet.AddAttributes(Pet.POINTS,- item.getIncrPoints());
        pet.AddAttributes(Pet.SPEED,- item.getIncrSpeed());
        pet.AddAttributes(Pet.TONGUELEN,- item.getIncrTongueLen());
    }
    
    public void AddAttributes(int AttType, int Increment)
    {
        pet.AddAttributes(AttType, Increment);
    }
    public int GetAttributes(int AttType)
    {
        return pet.GetAttributes(AttType);
    }
    public void SetAttributes(int AttType, int Increment)
    {
        pet.SetAttributes(AttType, Increment);
    }
    
    /**
     * @return the speech
     */
    public int getSpeech() {
        return pet.getSpeech();
    }

    /**
     * @param speech the speech to set
     */
    public void setSpeech(int speech) {
        pet.setSpeech(speech);
    }

    /**
     * @return the tongueLen
     */
    public int getTongueLen() {
        return pet.getTongueLen();
    }

    /**
     * @param tongueLen the tongueLen to set
     */
    public void setTongueLen(int tongueLen) {
        pet.setTongueLen(tongueLen);
    }
}
